package com.auto.learning.pay;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.alipay.sdk.net.MspClient;
import com.auto.learning.R;
import com.auto.learning.activity.BundingActivity;
import com.auto.learning.event.listener.DialogListener;
import com.auto.learning.net.HttpCommonInfo;
import com.auto.learning.net.HttpHelper;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.pay.alipay.AliPay;
import com.auto.learning.pay.wechatpay.WechatPayCommon;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.view.MyProgressDialog;
import com.auto.learning.view.SharedPreferencesUtils;


public class PayUtils {
	
	private Context context;
	private int platform;
	private int money;
	private MyProgressDialog dialog;
	public PayUtils(Context context,int platform, int money,int bookid){
		this.context = context;
		this.platform = platform;
		this.money = money;
		dialog = MyProgressDialog.createLoadingDialog(context);
		
	}
	public PayUtils(Context context,int platform, int money){
		this.context = context;
		this.platform = platform;
		this.money = money;
		dialog = MyProgressDialog.createLoadingDialog(context);
	}
	
	@SuppressLint("HandlerLeak") 
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
			switch (msg.what) {
				case 101:  //支付
					if (msg.arg1 == 1) {  //支付宝
						new AliPay(context, msg.obj.toString() , money);
					}else if (msg.arg1 == 2) {
						new WechatPayCommon(context, money).payByWeixin(msg.obj.toString());
					}
					break;
				case 102: //支付失败
					if (msg.obj != null) {
						CommonMethod.makeNotice(context, msg.obj.toString());
					}
					break;
				case 103:
					if (msg.obj != null) {
						CommonMethod.makeNotice(context, msg.obj.toString());
					}
					break;
				case -200:
					CommonMethod.showDialog(context, new String[]{"现在绑定", "取消"}, msg.obj.toString()+"", new DialogListener() {
						
						@Override
						public void otherButtonClick(Dialog dialog) {
							dialog.dismiss();
						}
						@Override
						public void okButtonClick(Dialog dialog) {
							dialog.dismiss();
							Intent intent = new Intent(context, BundingActivity.class);
							context.startActivity(intent);
						}
						
						@Override
						public void cancelButtonClick(Dialog dialog) {
							dialog.dismiss();
						}
					});
					break;
			}
		};
	};
	
	
	/**
	 * 微信或支付宝支付
	 * @param context
	 * @param money
	 * @param platform
	 * @param handler
	 */
	public void pay(){
		if (dialog != null) {
			dialog.show();
		}
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(context, HttpRequestHelper.USERID, 0))));
					params.add(new BasicNameValuePair("sessionId", MyUserInfo.getInstance().getUserInfoBean().getSessionId()));
					params.add(new BasicNameValuePair("platform",String.valueOf(platform)));
					params.add(new BasicNameValuePair("num",String.valueOf(money)));
					HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.PAY_BUY_RECHARGE, params,context);
					String result = HttpHelper.entity2String(entity);
					JSONObject resultJsonObject = new JSONObject(result);
					JSONObject resultDetail = resultJsonObject.getJSONObject(HttpCommonInfo.RESULT);
					int resultCode = resultDetail.getInt(HttpCommonInfo.RESULT_CODE);
					String msg = resultDetail.getString(HttpCommonInfo.RESULT_MSG);
					if (resultCode == 0) {
						JSONObject jsonObject = resultJsonObject.getJSONObject(HttpCommonInfo.DATA);
						Message message=new Message();
						message.what= 101;
						message.arg1 = platform ;
						message.obj = jsonObject.getString("payData");
						handler.sendMessage(message);
					}else if (resultCode == -200) {
						handler.obtainMessage(resultCode, msg).sendToTarget();
					}else {
						Message message=new Message();
						message.what= 103;
						message.obj = resultJsonObject.getString("msg");
						handler.sendMessage(message);
					}
				} catch (Exception e) {
					e.printStackTrace();
					Message message=new Message();
					message.what = 102;
					message.obj = context.getResources().getString(R.string.connect_err_io);
					handler.sendMessage(message);
				}
			}
		}).start();
	}
}
